package win.zxshuo.www.service;

import org.apache.juli.logging.Log;
import org.apache.juli.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.stereotype.Service;
import win.zxshuo.www.model.BookModel;
import win.zxshuo.www.model.chapters.*;
import win.zxshuo.www.repository.chapters.*;

import java.util.*;

@Service
@Configurable
public class ChapterService {

    Log logger = LogFactory.getLog(getClass());

    @Autowired
    ChapterRepository chapterRepository;
    @Autowired
    Chapter2Repository chapter2Repository;
    @Autowired
    Chapter3Repository chapter3Repository;
    @Autowired
    Chapter4Repository chapter4Repository;
    @Autowired
    Chapter5Repository chapter5Repository;
    @Autowired
    Chapter6Repository chapter6Repository;
    @Autowired
    Chapter7Repository chapter7Repository;
    @Autowired
    Chapter8Repository chapter8Repository;
    @Autowired
    Chapter9Repository chapter9Repository;
    @Autowired
    Chapter10Repository chapter10Repository;

    @Autowired
    BookService bookService;

    /**
     * 获取可用的章节表，但是目前采用MongoDB 预计是不需要了。
     * @return
     */
    public Integer getAvailableChapterTable(){
        Integer lastChapterTable = bookService.getLatestChapterTable();
        if (lastChapterTable == null){
            lastChapterTable = 0;
        }
        Integer numOfBook = bookService.getBookChapterNum(lastChapterTable);
        if (numOfBook <5000){ // 5000本 每本大概1000章，一个章节表存储应该是500万条数据。对于小VPS来说很大了。如果你的机器够牛逼，也可以换成10000
            return lastChapterTable;
        }else {
            return lastChapterTable + 1;
        }
    }

    /**
      * @Description: 给书籍添加新章节
      * @Author: RffanLAB
      * @Params: bookModel,chapterModeList
      * @Returns:
      * @CreateDate: 2018/4/5
      */
    public void addNewChapterForBook(BookModel bookModel, List<ChapterModel> chapterModelList) {

        for (ChapterModel chapterModel:chapterModelList){
            chapterModel.setBookId(bookModel.getId());
            chapterModel.setCreateDate(new Date());
            chapterModel.setModifyDate(new Date());
            chapterModel.setSpiderStatus("0");
            switch (bookModel.getChapterTable()){
                case 0:
                    if (chapterRepository.findByBookIdAndSpiderUrlAndTitle(bookModel.getId(),chapterModel.getSpiderUrl(),chapterModel.getTitle()) != null){
                        break;
                    }
                    chapterRepository.save(chapterModel);
                    break;
                case 1:
                    if (chapter2Repository.findByBookIdAndSpiderUrlAndTitle(bookModel.getId(),chapterModel.getSpiderUrl(),chapterModel.getTitle()) != null){
                        break;
                    }
                    Chapter2Model chapter2Model = new Chapter2Model(chapterModel);
                    chapter2Repository.save(chapter2Model);
                    break;
                case 2:
                    if (chapter3Repository.findByBookIdAndSpiderUrlAndTitle(bookModel.getId(),chapterModel.getSpiderUrl(),chapterModel.getTitle()) != null){
                        break;
                    }
                    Chapter3Model chapter3Model = new Chapter3Model(chapterModel);
                    chapter3Repository.save(chapter3Model);
                    break;
                case 3:
                    if (chapter4Repository.findByBookIdAndSpiderUrlAndTitle(bookModel.getId(),chapterModel.getSpiderUrl(),chapterModel.getTitle()) != null){
                        break;
                    }
                    Chapter4Model chapter4Model = new Chapter4Model(chapterModel);
                    chapter4Repository.save(chapter4Model);
                    break;
                case 4:
                    if (chapter5Repository.findByBookIdAndSpiderUrlAndTitle(bookModel.getId(),chapterModel.getSpiderUrl(),chapterModel.getTitle()) != null){
                        break;
                    }
                    Chapter5Model chapter5Model = new Chapter5Model(chapterModel);
                    chapter5Repository.save(chapter5Model);
                    break;
                case 5:
                    if (chapter6Repository.findByBookIdAndSpiderUrlAndTitle(bookModel.getId(),chapterModel.getSpiderUrl(),chapterModel.getTitle()) != null){
                        break;
                    }
                    Chapter6Model chapter6Model = new Chapter6Model(chapterModel);
                    chapter6Repository.save(chapter6Model);
                    break;
                case 6:
                    if (chapter7Repository.findByBookIdAndSpiderUrlAndTitle(bookModel.getId(),chapterModel.getSpiderUrl(),chapterModel.getTitle()) != null){
                        break;
                    }
                    Chapter7Model chapter7Model = new Chapter7Model(chapterModel);
                    chapter7Repository.save(chapter7Model);
                    break;
                case 7:
                    if (chapter8Repository.findByBookIdAndSpiderUrlAndTitle(bookModel.getId(),chapterModel.getSpiderUrl(),chapterModel.getTitle()) != null){
                        break;
                    }
                    Chapter8Model chapter8Model = new Chapter8Model(chapterModel);
                    chapter8Repository.save(chapter8Model);
                    break;
                case 8:
                    if (chapter9Repository.findByBookIdAndSpiderUrlAndTitle(bookModel.getId(),chapterModel.getSpiderUrl(),chapterModel.getTitle()) != null){
                        break;
                    }
                    Chapter9Model chapter9Model = new Chapter9Model(chapterModel);
                    chapter9Repository.save(chapter9Model);
                    break;
                case 9:
                    if (chapter10Repository.findByBookIdAndSpiderUrlAndTitle(bookModel.getId(),chapterModel.getSpiderUrl(),chapterModel.getTitle()) != null){
                        break;
                    }
                    Chapter10Model chapter10Model = new Chapter10Model(chapterModel);
                    chapter10Repository.save(chapter10Model);
                    break;

            }
        }


    }

    /**
      * @Description: 更新章节
      * @Author: RffanLAB
      * @Params:
      * @Returns:
      * @CreateDate: 2018/4/5
      */
    public void updateChapterBy(ChapterModel chapterModel){
        chapterRepository.save(chapterModel);
    }

    public void updateChapterBy(ChapterModel chapterModel,BookModel bookModel){
        switch (bookModel.getChapterTable()){
            case 0:
                chapterRepository.save(chapterModel);
                break;
            case 1:
                chapter2Repository.save(new Chapter2Model(chapterModel));
                break;
            case 2:
                chapter3Repository.save(new Chapter3Model(chapterModel));
                break;
            case 3:
                chapter4Repository.save(new Chapter4Model(chapterModel));
                break;
            case 4:
                chapter5Repository.save(new Chapter5Model(chapterModel));
                break;
            case 5:
                chapter6Repository.save(new Chapter6Model(chapterModel));
                break;
            case 6:
                chapter7Repository.save(new Chapter7Model(chapterModel));
                break;
            case 7:
                chapter8Repository.save(new Chapter8Model(chapterModel));
                break;
            case 8:
                chapter9Repository.save(new Chapter9Model(chapterModel));
                break;
            case 9:
                chapter10Repository.save(new Chapter10Model(chapterModel));
                break;
                default:
                    break;
        }
    }

    /**
     * 获取所有未采集的章节，以100计
     * @return
     */
    public List<ChapterModel> getUncrawlerdUrl(){
        return chapterRepository.getUnCrawlerUrlLimitOnehundred();
    }

    public List<Chapter2Model> getUncrawlerdUrl2(){
        return chapter2Repository.getUnCrawlerUrlLimitOnehundred();
    }

    public List<Chapter3Model> getUncrawlerdUrl3(){
        return chapter3Repository.getUnCrawlerUrlLimitOnehundred();
    }

    public List<Chapter4Model> getUncrawlerdUrl4(){
        return chapter4Repository.getUnCrawlerUrlLimitOnehundred();
    }

    public List<Chapter5Model> getUncrawlerdUrl5(){
        return chapter5Repository.getUnCrawlerUrlLimitOnehundred();
    }
    public List<Chapter6Model> getUncrawlerdUrl6(){
        return chapter6Repository.getUnCrawlerUrlLimitOnehundred();
    }
    public List<Chapter7Model> getUncrawlerdUrl7(){
        return chapter7Repository.getUnCrawlerUrlLimitOnehundred();
    }
    public List<Chapter8Model> getUncrawlerdUrl8(){
        return chapter8Repository.getUnCrawlerUrlLimitOnehundred();
    }
    public List<Chapter9Model> getUncrawlerdUrl9(){
        return chapter9Repository.getUnCrawlerUrlLimitOnehundred();
    }
    public List<Chapter10Model> getUncrawlerdUrl10(){
        return chapter10Repository.getUnCrawlerUrlLimitOnehundred();
    }
    // 获取所有未采集章节的方法结束


    /**
     * 获取是为采集的章节
     * @param bookModel
     * @return
     */
    public List<ChapterModel> getBookUncrawlerUrl(BookModel bookModel){
        List<ChapterModel> chapterModels = new ArrayList<>();
        switch (bookModel.getChapterTable()){
            case 0:
                chapterModels = chapterRepository.getAllUncrawlerBookByBookId(bookModel.getId());
                break;
            case 1:
                chapterModels = translateOtherChapterModelToOriginChapterModel((List<Object>)(List<?>)chapter2Repository.getAllUncrawlerBookByBookId(bookModel.getId()),Chapter2Model.class);
                break;
            case 2:
                chapterModels = translateOtherChapterModelToOriginChapterModel((List<Object>)(List<?>)chapter3Repository.getAllUncrawlerBookByBookId(bookModel.getId()),Chapter3Model.class);
                break;
            case 3:
                chapterModels = translateOtherChapterModelToOriginChapterModel((List<Object>)(List<?>)chapter4Repository.getAllUncrawlerBookByBookId(bookModel.getId()),Chapter4Model.class);
                break;
            case 4:
                chapterModels = translateOtherChapterModelToOriginChapterModel((List<Object>)(List<?>)chapter5Repository.getAllUncrawlerBookByBookId(bookModel.getId()),Chapter5Model.class);
                break;
            case 5:
                chapterModels = translateOtherChapterModelToOriginChapterModel((List<Object>)(List<?>)chapter6Repository.getAllUncrawlerBookByBookId(bookModel.getId()),Chapter6Model.class);
                break;
            case 6:
                chapterModels = translateOtherChapterModelToOriginChapterModel((List<Object>)(List<?>)chapter7Repository.getAllUncrawlerBookByBookId(bookModel.getId()),Chapter7Model.class);
                break;
            case 7:
                chapterModels = translateOtherChapterModelToOriginChapterModel((List<Object>)(List<?>)chapter8Repository.getAllUncrawlerBookByBookId(bookModel.getId()),Chapter8Model.class);
                break;
            case 8:
                chapterModels = translateOtherChapterModelToOriginChapterModel((List<Object>)(List<?>)chapter9Repository.getAllUncrawlerBookByBookId(bookModel.getId()),Chapter9Model.class);
                break;
            case 9:
                chapterModels = translateOtherChapterModelToOriginChapterModel((List<Object>)(List<?>)chapter10Repository.getAllUncrawlerBookByBookId(bookModel.getId()),Chapter10Model.class);
                break;
                default:
                    break;
        }
        return chapterModels;
    }




    // 更新已经采集的章节

    // 更新已经采集的章节


    public List<ChapterModel> translateOtherChapterModelToOriginChapterModel(List<Object> chapterLists,Class chapterClass){
        List<ChapterModel> chapterModels = new ArrayList<>();
        for (Object object:chapterLists){
            try {
                ChapterModel tmpChapterModel = new ChapterModel(object,chapterClass);
                chapterModels.add(tmpChapterModel);
            }catch (Exception e){
                logger.info("转换为ChapterModel实体类的时候出错了");
                logger.error(e.getCause());
            }
        }
        return chapterModels;
    }


    /**
     * 通过书籍id获取所有章节
     * @param bookid
     * @param chapterTable
     * @return
     */
    public List<List<ChapterModel>> getAllChaptersBy(Long bookid,Integer chapterTable){
        //TODO 还要做每张表的数据
        List<ChapterModel> chapterModels = new ArrayList<>();
        switch (chapterTable){
            case 0:
                chapterModels = chapterRepository.findAllByBookId(bookid);
                break;
            case 1:
                List<Chapter2Model> list = chapter2Repository.findAllByBookId(bookid);
                // 直接的类型转换，这里我知道是这个类型。但是，你懂的，你需要检查一番。这里并不安全。
                chapterModels = translateOtherChapterModelToOriginChapterModel((List<Object>)(List<?>)list ,Chapter2Model.class);
                break;
            case 2:
                List<Chapter3Model> list3 = chapter3Repository.findAllByBookId(bookid);
                chapterModels = translateOtherChapterModelToOriginChapterModel((List<Object>)(List<?>)list3,Chapter2Model.class);
                break;
            case 3:
                List<Chapter4Model> list4 = chapter4Repository.findAllByBookId(bookid);
                chapterModels = translateOtherChapterModelToOriginChapterModel((List<Object>)(List<?>)list4,Chapter2Model.class);
                break;
            case 4:
                List<Chapter5Model> list5 = chapter5Repository.findAllByBookId(bookid);
                chapterModels = translateOtherChapterModelToOriginChapterModel((List<Object>)(List<?>)list5,Chapter2Model.class);
                break;
            case 5:
                List<Chapter6Model> list6 = chapter6Repository.findAllByBookId(bookid);
                chapterModels = translateOtherChapterModelToOriginChapterModel((List<Object>)(List<?>)list6,Chapter2Model.class);
                break;
            case 6:
                List<Chapter7Model> list7 = chapter7Repository.findAllByBookId(bookid);
                chapterModels = translateOtherChapterModelToOriginChapterModel((List<Object>)(List<?>)list7,Chapter2Model.class);
                break;
            case 7:
                List<Chapter8Model> list8 = chapter8Repository.findAllByBookId(bookid);
                chapterModels = translateOtherChapterModelToOriginChapterModel((List<Object>)(List<?>)list8,Chapter2Model.class);
                break;
            case 8:
                List<Chapter9Model> list9 = chapter9Repository.findAllByBookId(bookid);
                chapterModels = translateOtherChapterModelToOriginChapterModel((List<Object>)(List<?>)list9,Chapter2Model.class);
                break;
            case 9:
                List<Chapter10Model> list10 = chapter10Repository.findAllByBookId(bookid);
                chapterModels = translateOtherChapterModelToOriginChapterModel((List<Object>)(List<?>)list10,Chapter2Model.class);
                break;
        }
        return makeToThreePerList(chapterModels);
    }


    public List<ChapterModel> getAllChaptersBy(BookModel bookModel){
        List<ChapterModel> chapterModels = new ArrayList<>();
        switch (bookModel.getChapterTable()){
            case 0:
                chapterModels = chapterRepository.findAllByBookId(bookModel.getId());
                break;
            case 1:
                List<Chapter2Model> list = chapter2Repository.findAllByBookId(bookModel.getId());
                // 直接的类型转换，这里我知道是这个类型。但是，你懂的，你需要检查一番。这里并不安全。
                chapterModels = translateOtherChapterModelToOriginChapterModel((List<Object>)(List<?>)list ,Chapter2Model.class);
                break;
            case 2:
                List<Chapter3Model> list3 = chapter3Repository.findAllByBookId(bookModel.getId());
                chapterModels = translateOtherChapterModelToOriginChapterModel((List<Object>)(List<?>)list3,Chapter2Model.class);
                break;
            case 3:
                List<Chapter4Model> list4 = chapter4Repository.findAllByBookId(bookModel.getId());
                chapterModels = translateOtherChapterModelToOriginChapterModel((List<Object>)(List<?>)list4,Chapter2Model.class);
                break;
            case 4:
                List<Chapter5Model> list5 = chapter5Repository.findAllByBookId(bookModel.getId());
                chapterModels = translateOtherChapterModelToOriginChapterModel((List<Object>)(List<?>)list5,Chapter2Model.class);
                break;
            case 5:
                List<Chapter6Model> list6 = chapter6Repository.findAllByBookId(bookModel.getId());
                chapterModels = translateOtherChapterModelToOriginChapterModel((List<Object>)(List<?>)list6,Chapter2Model.class);
                break;
            case 6:
                List<Chapter7Model> list7 = chapter7Repository.findAllByBookId(bookModel.getId());
                chapterModels = translateOtherChapterModelToOriginChapterModel((List<Object>)(List<?>)list7,Chapter2Model.class);
                break;
            case 7:
                List<Chapter8Model> list8 = chapter8Repository.findAllByBookId(bookModel.getId());
                chapterModels = translateOtherChapterModelToOriginChapterModel((List<Object>)(List<?>)list8,Chapter2Model.class);
                break;
            case 8:
                List<Chapter9Model> list9 = chapter9Repository.findAllByBookId(bookModel.getId());
                chapterModels = translateOtherChapterModelToOriginChapterModel((List<Object>)(List<?>)list9,Chapter2Model.class);
                break;
            case 9:
                List<Chapter10Model> list10 = chapter10Repository.findAllByBookId(bookModel.getId());
                chapterModels = translateOtherChapterModelToOriginChapterModel((List<Object>)(List<?>)list10,Chapter2Model.class);
                break;
        }
        return chapterModels;
    }

    public List<Map<String,Object>> translateBookModelToListMap(List<ChapterModel> chapterModels) {
        List<Map<String,Object>> chapterModelList = new ArrayList<>();
        for (ChapterModel chapterModel:chapterModels){
            Map<String,Object> map = new HashMap<>();
            map.put("chapterid",chapterModel.getId());
            map.put("chapterTitle",chapterModel.getTitle());
            chapterModelList.add(map);
        }
        return chapterModelList;
    }


    /**
      * @Description: 通过书籍id和章节id来获取章节内容
      * @Author: RffanLAB
      * @Params: 
      * @Returns: 
      * @CreateDate: 2018/4/5
      */
    public ChapterModel getBy(Long bookid,Long chapterId) throws Exception{
        BookModel bookModel = bookService.getBookById(bookid);
        if (bookModel == null){
            throw new  Exception("不存在该书籍，请求错误");
        }
        switch (bookModel.getChapterTable()){
            case 0:
                return chapterRepository.findById(chapterId).get();
            case 1:
                Chapter2Model chapter2Model = chapter2Repository.findById(chapterId).get();
                if (chapter2Model != null){
                    return new ChapterModel(chapter2Model);
                }
                break;
            case 2:
                Chapter3Model chapter3Model = chapter3Repository.findById(chapterId).get();
                if (chapter3Model != null){
                    return new ChapterModel(chapter3Model);
                }
                break;
            case 3:
                Chapter4Model chapter4Model = chapter4Repository.findById(chapterId).get();
                if (chapter4Model != null){
                    return new ChapterModel(chapter4Model);
                }
                break;
            case 4:
                Chapter5Model chapter5Model = chapter5Repository.findById(chapterId).get();
                if (chapter5Model != null){
                    return new ChapterModel(chapter5Model);
                }
                break;
            case 5:
                Chapter6Model chapter6Model = chapter6Repository.findById(chapterId).get();
                if (chapter6Model != null){
                    return new ChapterModel(chapter6Model);
                }
                break;
            case 6:
                Chapter7Model chapter7Model = chapter7Repository.findById(chapterId).get();
                if (chapter7Model != null){
                    return new ChapterModel(chapter7Model);
                }
                break;
            case 7:
                Chapter8Model chapter8Model = chapter8Repository.findById(chapterId).get();
                if (chapter8Model != null){
                    return new ChapterModel(chapter8Model);
                }
                break;
            case 8:
                Chapter9Model chapter9Model = chapter9Repository.findById(chapterId).get();
                if (chapter9Model != null){
                    return new ChapterModel(chapter9Model);
                }
                break;
            case 9:
                Chapter10Model chapter10Model = chapter10Repository.findById(chapterId).get();
                if (chapter10Model != null){
                    return new ChapterModel(chapter10Model);
                }
                break;
        }
        return null;
    }


    /**
     * 获取章节列表，以三个为一组
     * @param chapterModelList
     * @return
     */
    public List<List<ChapterModel>> makeToThreePerList(List<ChapterModel> chapterModelList){
        List<List<ChapterModel>> newChapterLists = new ArrayList<>();
        Integer mark = 0;
        List<ChapterModel> tmpChapter = new ArrayList<>();
        for (ChapterModel chapterModel:chapterModelList){
            if (mark == 0){
                tmpChapter = new ArrayList<>();
            }
            tmpChapter.add(chapterModel);
            mark = mark + 1;
            if (mark == 3){
                newChapterLists.add(tmpChapter);
                mark = 0;
            }
        }
        return newChapterLists;
    }

    /**
     * 获取下一章的信息
     * @param chapterid
     * @param bookid
     * @return
     */
    public ChapterModel getNextChapter(Long chapterid,Long bookid){
        BookModel bookModel = bookService.getBookById(bookid);
        if (bookModel != null){
            switch (bookModel.getChapterTable()){
                case 0:
                    List<ChapterModel> chapterModels = chapterRepository.findNextChapterBy(bookid,chapterid);
                    if (chapterModels.size()>1){
                        return chapterModels.get(1);
                    }
                    break;
                case 1:
                    List<Chapter2Model> chapterModels2 = chapter2Repository.findNextChapterBy(bookid,chapterid);
                    if (chapterModels2.size()>1){
                        return new ChapterModel(chapterModels2.get(1));
                    }
                    break;
                case 2:
                    List<Chapter3Model> chapterModels3 = chapter3Repository.findNextChapterBy(bookid,chapterid);
                    if (chapterModels3.size()>1){
                        return new ChapterModel(chapterModels3.get(1));
                    }
                    break;
                case 3:
                    List<Chapter4Model> chapterModels4 = chapter4Repository.findNextChapterBy(bookid,chapterid);
                    if (chapterModels4.size()>1){
                        return new ChapterModel(chapterModels4.get(1));
                    }
                    break;
                case 4:
                    List<Chapter5Model> chapterModels5 = chapter5Repository.findNextChapterBy(bookid,chapterid);
                    if (chapterModels5.size()>1){
                        return new ChapterModel(chapterModels5.get(1));
                    }
                    break;
                case 5:
                    List<Chapter6Model> chapterModels6 = chapter6Repository.findNextChapterBy(bookid,chapterid);
                    if (chapterModels6.size()>1){
                        return new ChapterModel(chapterModels6.get(1));
                    }
                    break;
                case 6:
                    List<Chapter7Model> chapterModels7 = chapter7Repository.findNextChapterBy(bookid,chapterid);
                    if (chapterModels7.size()>1){
                        return new ChapterModel(chapterModels7.get(1));
                    }
                    break;
                case 7:
                    List<Chapter8Model> chapterModels8 = chapter8Repository.findNextChapterBy(bookid,chapterid);
                    if (chapterModels8.size()>1){
                        return new ChapterModel(chapterModels8.get(1));
                    }
                    break;
                case 8:
                    List<Chapter9Model> chapterModels9 = chapter9Repository.findNextChapterBy(bookid,chapterid);
                    if (chapterModels9.size()>1){
                        return new ChapterModel(chapterModels9.get(1));
                    }
                    break;
                case 9:
                    List<Chapter10Model> chapterModels10 = chapter10Repository.findNextChapterBy(bookid,chapterid);
                    if (chapterModels10.size()>1){
                        return new ChapterModel(chapterModels10.get(1));
                    }
                    break;

            }
        }
        return null;
    }

    /**
     * 获取上一章的信息
     * @param chapterid
     * @param bookid
     * @return
     */
    public ChapterModel getPreviousChapter(Long chapterid,Long bookid){
        BookModel bookModel = bookService.getBookById(bookid);
        if (bookModel != null){
            switch (bookModel.getChapterTable()){
                case 0:
                    List<ChapterModel> chapterModels = chapterRepository.findPreviousChapterBy(bookid,chapterid);
                    if (chapterModels.size()>1){
                        return chapterModels.get(1);
                    }
                    break;
                case 1:
                    List<Chapter2Model> chapterModels2 = chapter2Repository.findPreviousChapterBy(bookid,chapterid);
                    if (chapterModels2.size()>1){
                        return new ChapterModel(chapterModels2.get(1));
                    }
                    break;
                case 2:
                    List<Chapter3Model> chapterModels3 = chapter3Repository.findPreviousChapterBy(bookid,chapterid);
                    if (chapterModels3.size()>1){
                        return new ChapterModel(chapterModels3.get(1));
                    }
                    break;
                case 3:
                    List<Chapter4Model> chapterModels4 = chapter4Repository.findPreviousChapterBy(bookid,chapterid);
                    if (chapterModels4.size()>1){
                        return new ChapterModel(chapterModels4.get(1));
                    }
                    break;
                case 4:
                    List<Chapter5Model> chapterModels5 = chapter5Repository.findPreviousChapterBy(bookid,chapterid);
                    if (chapterModels5.size()>1){
                        return new ChapterModel(chapterModels5.get(1));
                    }
                    break;
                case 5:
                    List<Chapter6Model> chapterModels6 = chapter6Repository.findPreviousChapterBy(bookid,chapterid);
                    if (chapterModels6.size()>1){
                        return new ChapterModel(chapterModels6.get(1));
                    }
                    break;
                case 6:
                    List<Chapter7Model> chapterModels7 = chapter7Repository.findPreviousChapterBy(bookid,chapterid);
                    if (chapterModels7.size()>1){
                        return new ChapterModel(chapterModels7.get(1));
                    }
                    break;
                case 7:
                    List<Chapter8Model> chapterModels8 = chapter8Repository.findPreviousChapterBy(bookid,chapterid);
                    if (chapterModels8.size()>1){
                        return new ChapterModel(chapterModels8.get(1));
                    }
                    break;
                case 8:
                    List<Chapter9Model> chapterModels9 = chapter9Repository.findPreviousChapterBy(bookid,chapterid);
                    if (chapterModels9.size()>1){
                        return new ChapterModel(chapterModels9.get(1));
                    }
                    break;
                case 9:
                    List<Chapter10Model> chapterModels10 = chapter10Repository.findPreviousChapterBy(bookid,chapterid);
                    if (chapterModels10.size()>1){
                        return new ChapterModel(chapterModels10.get(1));
                    }
                    break;
            }
        }
        return null;
    }

    /**
     * 获取书籍的第一个章节
     * @param bookid
     * @return
     */
    public ChapterModel getFirstChapter(Long bookid){
        BookModel bookModel = bookService.getBookById(bookid);
        switch (bookModel.getChapterTable()){
            case 0:
                ChapterModel chapterModel = chapterRepository.getFirstChapterByBookid(bookid);
                return chapterModel;
            case 1:
                Chapter2Model chapter2Model = chapter2Repository.getFirstChapterByBookid(bookid);
                if (chapter2Model != null){
                    return new ChapterModel(chapter2Model);
                }
                break;
            case 2:
                Chapter3Model chapter3Model = chapter3Repository.getFirstChapterByBookid(bookid);
                if (chapter3Model != null){
                    return new ChapterModel(chapter3Model);
                }
                break;
            case 3:
                Chapter4Model chapter4Model = chapter4Repository.getFirstChapterByBookid(bookid);
                if (chapter4Model != null){
                    return new ChapterModel(chapter4Model);
                }
                break;
            case 4:
                Chapter5Model chapter5Model = chapter5Repository.getFirstChapterByBookid(bookid);
                if (chapter5Model != null){
                    return new ChapterModel(chapter5Model);
                }
                break;
            case 5:
                Chapter6Model chapter6Model = chapter6Repository.getFirstChapterByBookid(bookid);
                if (chapter6Model != null){
                    return new ChapterModel(chapter6Model);
                }
                break;
            case 6:
                Chapter7Model chapter7Model = chapter7Repository.getFirstChapterByBookid(bookid);
                if (chapter7Model != null){
                    return new ChapterModel(chapter7Model);
                }
                break;
            case 7:
                Chapter8Model chapter8Model = chapter8Repository.getFirstChapterByBookid(bookid);
                if (chapter8Model != null){
                    return new ChapterModel(chapter8Model);
                }
                break;
            case 8:
                Chapter9Model chapter9Model = chapter9Repository.getFirstChapterByBookid(bookid);
                if (chapter9Model != null){
                    return new ChapterModel(chapter9Model);
                }
                break;
            case 9:
                Chapter10Model chapter10Model = chapter10Repository.getFirstChapterByBookid(bookid);
                if (chapter10Model != null){
                    return new ChapterModel(chapter10Model);
                }
                break;
        }
        return null;
    }

    /**
     * 获取书籍的第一个章节
     * @param bookModel
     * @return
     */
    public ChapterModel getFirstChapter(BookModel bookModel){
        Long bookid = bookModel.getId();
        switch (bookModel.getChapterTable()){
            case 0:
                ChapterModel chapterModel = chapterRepository.getFirstChapterByBookid(bookid);
                return chapterModel;
            case 1:
                Chapter2Model chapter2Model = chapter2Repository.getFirstChapterByBookid(bookid);
                if (chapter2Model != null){
                    return new ChapterModel(chapter2Model);
                }
                break;
            case 2:
                Chapter3Model chapter3Model = chapter3Repository.getFirstChapterByBookid(bookid);
                if (chapter3Model != null){
                    return new ChapterModel(chapter3Model);
                }
                break;
            case 3:
                Chapter4Model chapter4Model = chapter4Repository.getFirstChapterByBookid(bookid);
                if (chapter4Model != null){
                    return new ChapterModel(chapter4Model);
                }
                break;
            case 4:
                Chapter5Model chapter5Model = chapter5Repository.getFirstChapterByBookid(bookid);
                if (chapter5Model != null){
                    return new ChapterModel(chapter5Model);
                }
                break;
            case 5:
                Chapter6Model chapter6Model = chapter6Repository.getFirstChapterByBookid(bookid);
                if (chapter6Model != null){
                    return new ChapterModel(chapter6Model);
                }
                break;
            case 6:
                Chapter7Model chapter7Model = chapter7Repository.getFirstChapterByBookid(bookid);
                if (chapter7Model != null){
                    return new ChapterModel(chapter7Model);
                }
                break;
            case 7:
                Chapter8Model chapter8Model = chapter8Repository.getFirstChapterByBookid(bookid);
                if (chapter8Model != null){
                    return new ChapterModel(chapter8Model);
                }
                break;
            case 8:
                Chapter9Model chapter9Model = chapter9Repository.getFirstChapterByBookid(bookid);
                if (chapter9Model != null){
                    return new ChapterModel(chapter9Model);
                }
                break;
            case 9:
                Chapter10Model chapter10Model = chapter10Repository.getFirstChapterByBookid(bookid);
                if (chapter10Model != null){
                    return new ChapterModel(chapter10Model);
                }
                break;
        }
        return null;
    }


    public ChapterModel getLastChapter(BookModel bookModel){
        Long bookid = bookModel.getId();
        switch (bookModel.getChapterTable()){
            case 0:
                ChapterModel chapterModel = chapterRepository.getLastChapterByBookid(bookid);
                return chapterModel;
            case 1:
                Chapter2Model chapter2Model = chapter2Repository.getLastChapterByBookid(bookid);
                return new  ChapterModel(chapter2Model);
            case 2:
                Chapter3Model chapter3Model = chapter3Repository.getLastChapterByBookid(bookid);
                return new  ChapterModel(chapter3Model);
            case 3:
                Chapter4Model chapter4Model = chapter4Repository.getLastChapterByBookid(bookid);
                return new  ChapterModel(chapter4Model);
            case 4:
                Chapter5Model chapter5Model = chapter5Repository.getLastChapterByBookid(bookid);
                return new  ChapterModel(chapter5Model);
            case 5:
                Chapter6Model chapter6Model = chapter6Repository.getLastChapterByBookid(bookid);
                return new  ChapterModel(chapter6Model);
            case 6:
                Chapter7Model chapter7Model = chapter7Repository.getLastChapterByBookid(bookid);
                return new  ChapterModel(chapter7Model);
            case 7:
                Chapter8Model chapter8Model = chapter8Repository.getLastChapterByBookid(bookid);
                return new  ChapterModel(chapter8Model);
            case 8:
                Chapter9Model chapter9Model = chapter9Repository.getLastChapterByBookid(bookid);
                return new  ChapterModel(chapter9Model);
            case 9:
                Chapter10Model chapter10Model = chapter10Repository.getLastChapterByBookid(bookid);
                return new  ChapterModel(chapter10Model);
        }
        return null;
    }

    /**
     * 获取书籍最新的三个章节
     * @param bookid
     * @return
     */
    public List<ChapterModel> getBookLastThreeChapters(Long bookid) throws Exception{
        BookModel bookModel = bookService.getBookById(bookid);
        if (bookModel == null){
            throw new Exception("书籍未找到");
        }
        switch (bookModel.getChapterTable()){
            case 0:
                List<ChapterModel> chapterModel = chapterRepository.getLastThreeChapterByBookId(bookModel.getId());
                return chapterModel;
            case 1:
                List<Chapter2Model> chapter2Model = chapter2Repository.getLastThreeChapterByBookId(bookModel.getId());
                return ChapterModel.makeChapter2List(chapter2Model);
            case 2:
                List<Chapter3Model> chapter3Model = chapter3Repository.getLastThreeChapterByBookId(bookModel.getId());
                return  ChapterModel.makeChapter3List(chapter3Model);
            case 3:
                List<Chapter4Model> chapter4Model = chapter4Repository.getLastThreeChapterByBookId(bookModel.getId());
                return  ChapterModel.makeChapter4List(chapter4Model);
            case 4:
                List<Chapter5Model> chapter5Model = chapter5Repository.getLastThreeChapterByBookId(bookModel.getId());
                return  ChapterModel.makeChapter5List(chapter5Model);
            case 5:
                List<Chapter6Model> chapter6Model = chapter6Repository.getLastThreeChapterByBookId(bookModel.getId());
                return ChapterModel.makeChapter6List(chapter6Model);
            case 6:
                List<Chapter7Model> chapter7Model = chapter7Repository.getLastThreeChapterByBookId(bookModel.getId());
                return ChapterModel.makeChapter7List(chapter7Model);
            case 7:
                List<Chapter8Model> chapter8Model = chapter8Repository.getLastThreeChapterByBookId(bookModel.getId());
                return ChapterModel.makeChapter8List(chapter8Model);
            case 8:
                List<Chapter9Model> chapter9Model = chapter9Repository.getLastThreeChapterByBookId(bookModel.getId());
                return ChapterModel.makeChapter9List(chapter9Model);
            case 9:
                List<Chapter10Model> chapter10Model = chapter10Repository.getLastThreeChapterByBookId(bookModel.getId());
                return ChapterModel.makeChapter10List(chapter10Model);
        }
        return null;
    }

    /**
     * 获取书籍的最新的三个章节
     * @param bookModel
     * @return
     */
    public List<ChapterModel> getBookLastThreeChapters(BookModel bookModel){
        switch (bookModel.getChapterTable()){
            case 0:
                List<ChapterModel> chapterModel = chapterRepository.getLastThreeChapterByBookId(bookModel.getId());
                return chapterModel;
            case 1:
                List<Chapter2Model> chapter2Model = chapter2Repository.getLastThreeChapterByBookId(bookModel.getId());
                return ChapterModel.makeChapter2List(chapter2Model);
            case 2:
                List<Chapter3Model> chapter3Model = chapter3Repository.getLastThreeChapterByBookId(bookModel.getId());
                return  ChapterModel.makeChapter3List(chapter3Model);
            case 3:
                List<Chapter4Model> chapter4Model = chapter4Repository.getLastThreeChapterByBookId(bookModel.getId());
                return  ChapterModel.makeChapter4List(chapter4Model);
            case 4:
                List<Chapter5Model> chapter5Model = chapter5Repository.getLastThreeChapterByBookId(bookModel.getId());
                return  ChapterModel.makeChapter5List(chapter5Model);
            case 5:
                List<Chapter6Model> chapter6Model = chapter6Repository.getLastThreeChapterByBookId(bookModel.getId());
                return ChapterModel.makeChapter6List(chapter6Model);
            case 6:
                List<Chapter7Model> chapter7Model = chapter7Repository.getLastThreeChapterByBookId(bookModel.getId());
                return ChapterModel.makeChapter7List(chapter7Model);
            case 7:
                List<Chapter8Model> chapter8Model = chapter8Repository.getLastThreeChapterByBookId(bookModel.getId());
                return ChapterModel.makeChapter8List(chapter8Model);
            case 8:
                List<Chapter9Model> chapter9Model = chapter9Repository.getLastThreeChapterByBookId(bookModel.getId());
                return ChapterModel.makeChapter9List(chapter9Model);
            case 9:
                List<Chapter10Model> chapter10Model = chapter10Repository.getLastThreeChapterByBookId(bookModel.getId());
                return ChapterModel.makeChapter10List(chapter10Model);
        }
        return null;
    }


    public void testSaving(){
        Chapter2Model chapter2Model = new Chapter2Model();
        chapter2Model.setCreateDate(new Date());
        Object object = (Object) chapter2Model;

    }


}
